'use strict'


const titbit = require(' titbit')
const {cookie,session} = require(' titbit-toolkit')
const token = require(' titbit-token')

let tok = new token({
    //token有效期，单位是秒
    expires:30,
    // 必须是32位字母数字
    key:'1qazxsw23edc4rfv5tgb6ynh7ujm8ikl',
    //必须是16位字母数字
    lv:'0okm9ijn8uhb7ygv'
})
const app = new titbit({
    debug:true,
    aaa:123,
})

app.use(async (c,next)=>{
    c.sendHeader('x-test-key',`${Math.random()}`)
    await next()
})
app.use(new cookie).use(new session)
// 注意一定要先启用cookie，在启用session

app.use(async (c, next) => {
    c.setHeader('x-test-key', `${Math.random()}`)
    await next()
})

app.use(async (c,next)=>{
    if(c.query.say === '12345'){
        await next()
    }
    else{
        c.status(403).send('deny')
    }
},{name:['home'],method:['GET']})

app.get('/',async c =>{
    c.send('ok')
},{name:'home',group:'home'})

app.get('/test',async c =>{

    c.send('test is Ok')
})
// app.get('/:x/:y',async c =>{
//     c.setSession('x',c.param.x)
//     c.setSession('y',c.param.y)
//     c.send(c.getSession())
// })



// 可以解密
app.get('/token',async c=>{
  let tk = tok.make({
      id:'123',
      user:'node',
      level:12
  })
  c.send(tk)
})
app.get('/decrypt/:token',async c =>{
    c.send(tok.verify(c.param.token))
})
app.run(1235)